package com.zlp.partice;

import com.zlp.utils.hashcode.DateUtil;
import org.junit.Test;

import java.util.*;

import static com.zlp.utils.hashcode.DateUtil.getDate;

/**
 * @author: zhulaiping
 * @time: 2022/1/23 14:01
 */
public class StockPrice {

    int curTime;
    Map<Integer,Integer> map;
    TreeMap<Integer,Integer> treeMap;
    public StockPrice() {
        map = new HashMap<>();
        treeMap = new TreeMap<>();
    }

    public void update(int timestamp, int price) {
        curTime = Math.max(curTime,timestamp);
        Integer val = map.get(timestamp);
        if(val != null){
            Integer cnt = treeMap.get(val);
            if(cnt == 1){
                treeMap.remove(val);
            }else {
                treeMap.put(val, cnt -1);
            }
        }
        map.put(timestamp,price);
        treeMap.put(price, treeMap.getOrDefault(price,0)+1);
    }

    public int current() {
        return map.get(curTime);
    }

    public int maximum() {
        return treeMap.lastKey();
    }

    public int minimum() {
        return treeMap.firstKey();
    }






    int[][] ps = new int[1001][1001];
    public void add(int[] point) {
       ps[point[0]][point[1]]++;
    }

    public int count(int[] point) {
        int x = point[0],y = point[1];
        int ret = 0;
        for (int i=0;i<1001;i++){
            if(i == x || ps[i][y] == 0) continue;
            int c1 = ps[i][y];
            int len = Math.abs(x - i);
            if(y-len>=0){
                ret += c1*ps[i][y-len]*ps[x][y-len];
            }
            ret += c1*ps[i][y+len]*ps[x][y+len];
        }
        return ret;
    }

    public int countValidWords(String sentence) {
        String[] arr = sentence.split(" ");
        int ret = 0;
        for (String str:arr){
            if(str.length()==0) continue;
            ret += judge(str.toCharArray());
        }
        return ret;
    }




    private int judge(char[] arr) {
        int cnt = 0;
        for (int i = 0; i < arr.length; i++) {
            //1.不能包含数字
            if (arr[i] >= '0' && arr[i] <= '9') return 0;
            //可以没有标点符号，有只能在末尾
            if ((arr[i] == '.' || arr[i] == ',' || arr[i] == '!') && i != arr.length - 1) return 0;
            //如果存在-，-不能再开头或末尾
            if (arr[i] == '-' && (i == 0 || i == arr.length - 1)) return 0;
            //如果存在-，-两边必须是字符
            if (arr[i] == '-' && ((arr[i - 1] < 'a' || arr[i - 1] > 'z') || (arr[i + 1] < 'a' || arr[i + 1] > 'z')))
                return 0;
            //如果存在-，-个数不能大于1
            if (arr[i] == '-') cnt++;
            if (cnt > 1) return 0;
        }
        return 1;
    }


    public int numberOfWeakCharacters(int[][] properties) {
        Arrays.sort(properties,(o1, o2) -> o1[0]!=o2[0]?o2[0]-o1[0]:o1[1]-o2[1]);
        int ret = 0,len = properties.length;
        int curDefend = properties[0][1];
        for (int i=1;i<len;i++){
           if(curDefend<properties[i][1]){
               ret ++;
           }
           curDefend = Math.max(curDefend,properties[i][1]);
        }
        return ret;
    }
    public String[] uncommonFromSentences(String s1, String s2) {
        String[] arr1 = s1.split(" ");
        String[] arr2 = s2.split(" ");
        Map<String,Integer> map1 = new HashMap<>();
        for (String str:arr1) map1.put(str,map1.getOrDefault(str,0)+1);
        Map<String,Integer> map2 = new HashMap<>();
        for (String str:arr2) map2.put(str,map2.getOrDefault(str,0)+1);
        Set<String> setAll = new HashSet<>(Arrays.asList(arr1));
        setAll.addAll(Arrays.asList(arr2));
        List<String> list = new ArrayList<>();
        for (String str:setAll){
            if(map1.containsKey(str)&&map2.containsKey(str)) continue;
            if(map1.containsKey(str)&&map1.get(str)>1) continue;
            if(map2.containsKey(str)&&map2.get(str)>1) continue;
            list.add(str);
        }
        return list.stream().toArray(String[]::new);

    }


    public String longestNiceSubstring(String s) {
        char[] arr = s.toCharArray();
        int len = arr.length;
        while (len>0){
            for (int j=0;j<=arr.length-len;j++){
                if(cal(arr,j,len)){
                    return new String(arr,j,len);
                }
            }
            len--;
        }
        return "";
    }

    private boolean cal(char[] arr, int j, int len) {
        Set<Character> set = new HashSet<>();
        for (int i=j;i<j+len;i++) set.add(arr[j]);
        for (int i=j;i<j+len;i++){
            if (arr[i]>='a'&&arr[i]<='z' && !set.contains((char)(arr[i]-32))){
                return false;
            }
            if (arr[i]>='A'&&arr[i]<='Z' && !set.contains((char)(arr[i]+32))){
                return false;
            }
        }
        return true;
    }



    public String longestDiverseString(int a, int b, int c) {
        int[][] map = new int[][]{{'a',a},{'b',b},{'c',c}};
        PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> o2[1]-o1[1]);
        StringBuilder ret = new StringBuilder();
        for (int[] cur:map){
            if(cur[1]>0){
                queue.offer(cur);
            }
        }
        while (true && queue.size()>0){
            int[] cur = queue.poll();
            int len = ret.length();
            if(len > 2 && ret.charAt(len-1) == cur[0] && ret.charAt(len-2) == cur[0]){
                if(queue.size()==0) break;
                int[] cur2 = queue.poll();
                ret.append((char)cur2[0]);
                cur2[1]--;
                if(cur2[1]>0){
                    queue.offer(cur2);
                }
                queue.offer(cur);

            }else {
                ret.append((char)cur[0]);
                cur[1]--;
                if(cur[1]>0){
                    queue.offer(cur);
                }
            }
        }
        return ret.toString();
    }




    public int maxNumberOfBalloons(String text) {
        int[] index = {'a','b','n','o','l'};
        int[] map = new int[26];
        for (char ch:text.toCharArray()){
            map[ch]++;
        }
        int ret = Integer.MAX_VALUE;
        for (int i=0;i<index.length;i++){
            if(i<=2) ret = Math.min(ret,map[index[i]]);
            else ret = Math.min(ret,map[index[i]]/2);
        }
        return ret;
    }

    @Test
    public void ee(){
        //System.out.println(numberOfWeakCharacters(new int[][]{{1,5},{10,4},{4,3}}));
        //uncommonFromSentences("this apple is sweet","this apple is sour");
        //longestNiceSubstring("YazaAay");
        //System.out.println(longestDiverseString(2, 4, 1));
        minimumDifference(new int[]{9,4,1,7},2);
    }
    public List<Integer> luckyNumbers (int[][] matrix) {
        int row = matrix.length,column = matrix[0].length;
        List<Integer>list = new ArrayList<>();
        for (int i=0;i<row;i++){
            int index = 0,value = Integer.MAX_VALUE;
            for (int j=0;j<column;j++){
                if(value>matrix[i][j]){
                    value = matrix[i][j];
                    index = j;
                }
            }
            if(isMax(matrix,i,index)){
                list.add(matrix[i][index]);
            }
        }
        return list;
    }

    private boolean isMax(int[][] matrix, int i, int index) {
        for (int j=0;j<matrix.length;j++){
            if(j == i) continue;
            if(matrix[j][index]>matrix[i][index])
                return false;
        }
        return true;
    }

    public int minimumDifference(int[] nums, int k) {
        if(k==1||nums.length==0) return 0;
        Arrays.sort(nums);
        int ret = Integer.MAX_VALUE;
        for (int i=0;i<=nums.length-k;i++){
            ret = Math.min(ret,nums[i+k-1]-nums[i]);
        }
        return ret;
    }
    public int findMinFibonacciNumbers(int k) {
        int[] f = new int[45];
        f[1] = 1;
        for (int i=2;i<f.length;i++) {
            f[i] = f[i-1] + f[i-2];
        }
        int ret = 0;
        while (k!=0){
            for (int i=f.length-1;i>=0;i--){
                if(f[i]<=k){
                    k -= f[i];
                    ret++;
                    break;
                }
            }
        }
        return ret;
    }

    int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    public int getMaximumGold(int[][] grid) {
        int row = grid.length, column = grid[0].length;
        int ret = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (grid[i][j] == 0) continue;
                ret = Math.max(ret, dfs(i, j, grid));
            }
        }
        return ret;
    }
    private int dfs(int i, int j, int[][] grid) {
        if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) {
            return 0;
        }
        int cur = 0,temp = grid[i][j];
        grid[i][j] = 0;
        for (int[] direction : directions) {
            cur = Math.max(cur, dfs(i + direction[0], j + direction[1], grid));
        }
        grid[i][j] = temp;
        return cur + grid[i][j];
    }

    public int countKDifference(int[] nums, int k) {
        int ret = 0;
        for (int i=0;i<nums.length-1;i++){
            for (int j=i+1;j<nums.length;j++){
                if(Math.abs(nums[i]-nums[j])==k){
                    ret++;
                }
            }
        }
        return ret;
    }

    @Test
    public void aa(){
        Integer a = new Integer(1);
        ss(a);
        System.out.println(a);
    }

    public void ss(Integer a){
        a++;
    }

    public int maximumDifference(int[] nums) {
        int ret = -1,last = Integer.MAX_VALUE;
        for (int val:nums){
            if(last>val) last = val;
            if(val>last) ret = Math.max(ret,val-last);
        }
        return ret;
    }
    public String reverseOnlyLetters(String s) {
        char[] chars = s.toCharArray();
        for (int l = 0,r=chars.length-1;l<r;){
            if(Character.isLetter(chars[l])&&Character.isLetter(chars[r])){
                char temp = chars[l];
                chars[l] = chars[r];
                chars[r] = temp;
            }else if(Character.isLetter(chars[l])) r--;
            else if(Character.isLetter(chars[r])) l++;
            else {
                r--;
                l++;
            }
        }
        return new String(chars);
    }
    public int findCenter(int[][] edges) {
        Map<Integer, Integer>map = new HashMap<>();
        for (int[] arr:edges){
            map.put(arr[0],map.getOrDefault(arr[0],0)+1);
            map.put(arr[1],map.getOrDefault(arr[1],0)+1);
            if(map.get(arr[0])>1) return arr[0];
            if(map.get(arr[1])>1) return arr[1];
        }
        return 0;
    }
    public static int getWeekdayByDate(Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        int weekDay = instance.get(Calendar.DAY_OF_WEEK)-1;
        System.out.println(weekDay);
        return weekDay;
    }
    public static void main(String[] args) {

        getWeekdayByDate(DateUtil.getDateDay("2022-04-05"));  //周三
        getWeekdayByDate( DateUtil.addDate(new Date(),1));//周四
        getWeekdayByDate( DateUtil.addDate(new Date(),2));//周五
        getWeekdayByDate( DateUtil.addDate(new Date(),3));//周六
        getWeekdayByDate( DateUtil.addDate(new Date(),4));//周日
        getWeekdayByDate( DateUtil.addDate(new Date(),5));//周一
        getWeekdayByDate( DateUtil.addDate(new Date(),6));//周二
    }


}
